package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Sbxx;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class SbxxForm extends BasePage implements Serializable {
    private GenericManager<Sbxx, Long> sbxxManager;
    private Sbxx sbxx = new Sbxx();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setSbxxManager(GenericManager<Sbxx, Long> sbxxManager) {
        this.sbxxManager = sbxxManager;
    }

    public Sbxx getSbxx() {
        return sbxx;
    }

    public void setSbxx(Sbxx sbxx) {
        this.sbxx = sbxx;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        sbxxManager.remove(sbxx.getId());
        addMessage("sbxx.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            sbxx = sbxxManager.get(id);
        } else {
            sbxx = new Sbxx();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (sbxx.getId() == null || sbxx.getId() == 0);
        sbxx.setJbqk( jbqk );
        sbxx.setUsername(getRequest().getRemoteUser());
        sbxx.setOptdate(new Date());
        sbxx.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        
        sbxxManager.save(sbxx);

        String key = (isNew) ? "sbxx.added" : "sbxx.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = sbxx.getId();
            return "edit";
        }
    }
} 